package com.foreknow.service.impl;

import com.foreknow.dao.impl.FoodDaoImpl;
import com.foreknow.db.DBManager;
import com.foreknow.db.JdbcTemplate;
import com.foreknow.model.Food;
import com.foreknow.service.FoodService;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-08-14 11:08
 */
public class FoodServiceImpl implements FoodService {
    private FoodDaoImpl dao=new FoodDaoImpl();
    @Override
    public List<Object> showAll()  {

        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);

        List<Object> queryAll = null;
        try {
            queryAll = dao.getQueryAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return queryAll;
    }

    @Override
    public boolean add(Food food) {
        boolean save=false;
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);
        try {
             save = dao.save(food);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return save;
    }

    @Override
    public boolean del(int id) {
        boolean isdel=false;
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);
        try {
            isdel= dao.delete(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return isdel;
    }

    @Override
    public Object getQueryById(int id) {
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);

        try {
            Object queryById = dao.getQueryById(id);

            return  queryById;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public boolean Update(Food food) {
        boolean idUpdate=false;
        DBManager instance = DBManager.getInstance();
        Connection connection = instance.getConnection();
        dao.setConnection(connection);

        try {
            idUpdate = dao.Updatedao(food);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
